package com.yycx.module.bus.provider.strategy.impl;

import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.groovy.provider.script.MyGroovyScriptEngine;
import com.yycx.module.bus.client.entity.hy.BusPolicyPosition;
import com.yycx.module.bus.client.entity.hy.BusPolicySaleman;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import com.yycx.module.bus.provider.service.hy.BusPolicyPositionService;
import com.yycx.module.bus.provider.service.hy.BusPolicySalemanService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.bus.provider.strategy.SettleStrategy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 商务总负责人(省级)结算规则
 *
 * @author zyf
 */
@Service("settleStrategyE")
public class SettleStrategyE implements SettleStrategy {

    @Resource
    private BusSalemanService saleManService;

    @Resource
    private BusPolicyPositionService policyPositionService;

    @Resource
    private BusPolicySalemanService policySalemanService;

    @Resource
    private MyGroovyScriptEngine groovyScriptEngine;

    @Override
    public void setSettlePolicy(HyOrderDetail orderDetail, HyOrder hyOrder, Long saleManId) {
        //自定义结算政策
        Integer customPolicy = FlymeUtils.getInteger(orderDetail.getCustomPolicy(), 0);
        //是否享受省级结算
        Integer proLevel = FlymeUtils.getInteger(hyOrder.getProLevel(), 0);
        //结算规则(回款后结算)
        Integer settleRule = 1;
        Integer baopiao = 1;
        Integer hasTaxRateDiff=FlymeUtils.getInteger(hyOrder.getHasTaxRateDiff(),1);
        if (proLevel.equals(1)) {
            //发货日期
            Date sendDate = orderDetail.getSendDate();
            //结算政策ID
            Long policyDrugId = orderDetail.getPolicyDrugId();
            BigDecimal settlePrice = orderDetail.getSettlePriceE();
            BigDecimal settleRate = orderDetail.getSettleRateE();
            if (customPolicy.equals(0)) {
                //获取职务结算政策
                BusPolicyPosition busPolicyPosition = policyPositionService.findBusPolicyPosition(policyDrugId, 2L, sendDate);
                if (FlymeUtils.isNotEmpty(busPolicyPosition)) {
                    settlePrice = FlymeUtils.getBigDecimal(busPolicyPosition.getPositionPrice(), settlePrice);
                    settleRate = FlymeUtils.getBigDecimal(busPolicyPosition.getPositionRate(), settleRate);
                }
                //获取业务员结算政策
                BusPolicySaleman policySaleMan = policySalemanService.findByPolicyDrugIdAndSaleManId(policyDrugId, saleManId, sendDate);
                if (FlymeUtils.isNotEmpty(policySaleMan)) {
                    settlePrice = FlymeUtils.getBigDecimal(policySaleMan.getSaleManPrice(), settlePrice);
                    settleRate = FlymeUtils.getBigDecimal(policySaleMan.getSaleManRate(), settleRate);
                   /* if (FlymeUtils.isNotEmpty(policySaleMan.getSaleManSettleFormula())) {
                        settleFormulaContent = policySaleMan.getSaleManSettleFormula();
                    }*/
                    //价格取值方式
                    Integer priceType = FlymeUtils.getInteger(policySaleMan.getPriceType(), 1);
                    if (priceType.equals(2)) {
                        //使用成本价
                        settlePrice = FlymeUtils.getBigDecimal(orderDetail.getCostPrice(), settlePrice);
                    }
                    //结算规则(回款后结算)
                    settleRule = FlymeUtils.getInteger(policySaleMan.getSettleRule(), 1);
                    //是否包票(默认包票)
                    baopiao = FlymeUtils.getInteger(policySaleMan.getBaopiao(), 1);
                }
            }
            orderDetail.setBaopiao(baopiao);
            orderDetail.setSettleRule(settleRule);
            orderDetail.setSettlePriceE(settlePrice);
            orderDetail.setSettleRateE(settleRate);
            orderDetail.setSaleManIdE(saleManId);
            String positionSettleFormula = "(销售单价-商务总负责人结算价)*数量*(1-商务总负责人标准)";
            String deptProfitAmountFormula = "(商务总负责人结算价-部门结算价)*数量*(1-部门标准);";
            //税率差公式
            String taxRateDiffFormula = "(销售单价-商务总负责人结算价)*数量*((1-部门标准)-(1-商务总负责人标准));";
            BigDecimal settlePriceF = orderDetail.getSettlePrice();
            BigDecimal settlePriceD = orderDetail.getSettlePriceD();
            BigDecimal settlePriceC = orderDetail.getSettlePriceC();
            if (FlymeUtils.isNotEmpty(settlePriceF) && FlymeUtils.gtzero(settlePriceF)) {
                positionSettleFormula = positionSettleFormula + "-渠道商结算金额;";
            }
            if (FlymeUtils.isNotEmpty(settlePriceD) && FlymeUtils.gtzero(settlePriceD)) {
                positionSettleFormula = positionSettleFormula + "-商务经理结算金额;";
            }
            if (FlymeUtils.isNotEmpty(settlePriceC) && FlymeUtils.gtzero(settlePriceC)) {
                positionSettleFormula = positionSettleFormula + "-片区服务商结算金额;";
            }
            String formulaContent = positionSettleFormula;
            if (customPolicy.equals(1)) {
                formulaContent = orderDetail.getSettleFormulaContentE();
                deptProfitAmountFormula = orderDetail.getDeptProfitAmountFormula();
                taxRateDiffFormula = orderDetail.getTaxRateDiffFormula();
            }
            orderDetail.setSettleFormulaContentE(formulaContent);
            //部门利润公式
            orderDetail.setDeptProfitAmountFormula(deptProfitAmountFormula);
            if(FlymeUtils.gtzero(settleRate)&& hasTaxRateDiff.equals(1)) {
                //税率差公式
                orderDetail.setTaxRateDiffFormula(taxRateDiffFormula);
            }else{
                orderDetail.setTaxRateDiffFormula(null);
            }
        }
    }

    @Override
    public void setSettleAmount(HyOrderDetail hyOrderDetail, HyOrder hyOrder, Map<String, Object> variable) {
        //商务总负责人结算价
        BigDecimal settlePriceE = FlymeUtils.getBigDecimal(hyOrderDetail.getSettlePriceE(), "0");
        //商务总负责人结算公式
        String settleFormulaE = hyOrderDetail.getSettleFormulaContentE();
        //商务总负责人结算金额
        BigDecimal settleAmountE = new BigDecimal("0");
        if (FlymeUtils.gtzero(settlePriceE)) {
            //销售数量
            BigDecimal saleNum = hyOrderDetail.getSaleNum();
            //销售价
            BigDecimal salePrice = hyOrderDetail.getSalePrice();
            //商务总负责人结算标准
            BigDecimal settleRateE = FlymeUtils.getBigDecimal(hyOrderDetail.getSettleRateE(), "0");
            //商务总负责人结算金额
            settleAmountE = (salePrice.subtract(settlePriceE)).multiply(saleNum).multiply(settleRateE);

            //使用公式重新计算商务总负责人结算公式
            if (FlymeUtils.isNotEmpty(settleFormulaE)) {
                settleAmountE = groovyScriptEngine.executeBigDecimal(settleFormulaE, variable,2);
            }
        }
        if (FlymeUtils.isNotEmpty(settleFormulaE)) {
            settleAmountE = groovyScriptEngine.executeBigDecimal(settleFormulaE, variable,2);
        }
        hyOrderDetail.setSettleAmountE(settleAmountE);
        variable.put("商务总负责人结算金额", settleAmountE);
    }
}
